<template>
  <div class="login-container">
    <el-form
      @submit.native.prevent
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
<!--        <div style="text-align:center;">-->
<!--          <el-image-->
<!--            style="width: 60px; height: 40px;"-->
<!--            :src="src"-->
<!--          /></div>-->
        <h3
          class="title"
          style="display: flex;align-items: center;justify-content: center; color: #1e6abc; font-size: 28px;line-height: 40px;"
        >
          光大银行智慧社区
        </h3>

        <el-form-item prop="userName">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="userName"
            v-model="loginForm.userName"
            placeholder="用户名"
            name="userName"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="密码"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-form-item>
          <span class="svg-container">
            <svg-icon icon-class="code" />
          </span>
          <el-input
            ref="verificationCode"
            v-model="loginForm.verificationCode"
            placeholder="请输入验证码"
            name="verificationCode"
            type="text"
            tabindex="1"
            autocomplete="on"
          />
          <span class="show-pwd" @click="sendVerificationImg">
            <img style="height: 40px; width: 80px" :src="'data:image/jpg;base64,'+imgSrc" alt="">
          </span>
        </el-form-item>

        <el-button
          :loading="loading"
          type="success"
          style="width:100%;margin-bottom:20px;background: #497ee8"
          @click.native.prevent="handleLogin"
        >登录
        </el-button>
<!--        <el-button-->
<!--          type="success"-->
<!--          style="width:100%;margin-bottom:20px;margin-left: 0; background: #98D898; color: #378642;"-->
<!--          @click.native.prevent="toRegister"-->
<!--        >注册-->
<!--        </el-button>-->
      </div>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { getStorage, removeStorage, setStorage } from '@/utils/auth'
import { getDate } from '@/utils/common'
import { sendVerificationImg } from '@/api/user'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码长度最少为8位'))
      } else {
        callback()
      }
    }
    return {
      src: require('@/assets/images/login-logo.png'),
      loginForm: {
        loginSource: 'web',
        userName: '',
        password: '',
        verificationCode: '',
        uuid: '' // 凭证
      },
      loginRules: {
        userName: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      switchValue: false,
      imgSrc: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    window.addEventListener('keyup', this.onSubmit, false)
    this.$nextTick(() => {
      this.sendVerificationImg()
    })
  },
  methods: {
    destroyed() {
      window.removeEventListener('keyup', this.onSubmit, false)
    },
    onSubmit(e) {
      // debugger
      if (e.keyCode == 13 || e.keyCode == 100) {
        this.handleLogin('form')
      }
    },
    // 获取验证码
    sendVerificationImg() {
      sendVerificationImg().then(res => {
        if (res.code === 0) {
          this.imgSrc = res.result.img
          this.loginForm.uuid = res.result.uuid
        }
      })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          if (!this.loginForm.verificationCode) {
            this.$message.error('请输入验证码')
            return
          }
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
            this.sendVerificationImg()
          })
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #fff;
$light_gray: #fff;
$cursor: black;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 6px 5px 6px 15px;
      color: black;
      height: 45px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: #FFFFFF;
    border-radius: 5px;
    height: 50px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url("../../assets/images/qd.png") no-repeat;
  background-size: 100% 100%;
  overflow: hidden;
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding:0 35px;
    //margin:auto;
    overflow: hidden;
  }

  .tips {
    //font-size: 14px;
    //color: #fff;
    //margin-bottom: 10px;
    //
    //span {
    //  &:first-of-type {
    //    margin-right: 16px;
    //  }
    //}
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #999999;

    .left {
      display: flex;
      justify-content: space-between;
      align-items: center;

      span {
        margin-left: 10px;
      }
    }

    .right {
      span {
        margin-left: 10px;
        cursor: pointer;
        padding-bottom: 10px;
        //border-bottom: 1px solid #ffffff;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    background: #F5F5F5;
    //opacity:0.5;
    padding:20px;
    border-radius: 10px;
    position: relative;
    //text-align: center;
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 10px auto 10px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .foot {
    position: fixed;
    left: 0;
    bottom: 10px;
    text-align: center;
    width: 100%;
    color: #FFFFFF;
    //padding-bottom: 10px;
    height: 70px;

    .sp {
      display: inline-block;
      width: 100%;
      height: 20px;
      line-height: 20px;
      font-size: 14px;
    }
  }
}
</style>
